package com.xunge.controller;

import com.xunge.anno.Log;
import com.xunge.pojo.HomeworkSubmission;
import com.xunge.pojo.HomeworkSubmissionQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.pojo.Result;
import com.xunge.service.HomeworkSubmissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/homework-submission")
@RestController
public class HomeworkSubmissionController {
    @Autowired
    private HomeworkSubmissionService homeworkSubmissionService;

    /**
     * 分页查询作业提交信息
     */
    @GetMapping
    public Result page(HomeworkSubmissionQueryParam homeworkSubmissionQueryParam){
        log.info("分页请求参数:{}" ,homeworkSubmissionQueryParam);
        PageResult<HomeworkSubmission> pageResult = homeworkSubmissionService.page(homeworkSubmissionQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 保存作业提交
     */
    @Log
    @PostMapping
    public Result save(@RequestBody HomeworkSubmission homeworkSubmission){
        log.info("保存信息：{}",homeworkSubmission);
        homeworkSubmissionService.save(homeworkSubmission);
        return Result.success();
    }

    /**
     * 批量删除作业提交
     */
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("根据ID批量删除作业提交: {}",ids);
        homeworkSubmissionService.delete(ids);
        return Result.success();
    }

    /**
     * 根据ID查询作业提交
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据ID查询作业提交: {}",id);
        HomeworkSubmission homeworkSubmission = homeworkSubmissionService.getById(id);
        return Result.success(homeworkSubmission);
    }

    /**
     * 根据ID修改作业提交
     */
    @Log
    @PutMapping
    public Result update(@RequestBody HomeworkSubmission homeworkSubmission){
        log.info("根据ID修改作业提交：{}",homeworkSubmission);
        homeworkSubmissionService.update(homeworkSubmission);
        return Result.success();
    }

    /**
     * 根据作业ID查询提交记录
     */
    @GetMapping("/homework/{homeworkId}")
    public Result getByHomeworkId(@PathVariable Integer homeworkId){
        log.info("根据作业ID查询提交记录: {}",homeworkId);
        List<HomeworkSubmission> submissions = homeworkSubmissionService.getByHomeworkId(homeworkId);
        return Result.success(submissions);
    }

    /**
     * 根据学生ID查询提交记录
     */
    @GetMapping("/student/{studentId}")
    public Result getByStudentId(@PathVariable Integer studentId){
        log.info("根据学生ID查询提交记录: {}",studentId);
        List<HomeworkSubmission> submissions = homeworkSubmissionService.getByStudentId(studentId);
        return Result.success(submissions);
    }

    /**
     * 根据作业ID和学生ID查询提交记录
     */
    @GetMapping("/homework/{homeworkId}/student/{studentId}")
    public Result getByHomeworkIdAndStudentId(@PathVariable Integer homeworkId, @PathVariable Integer studentId){
        log.info("根据作业ID和学生ID查询提交记录: homeworkId={}, studentId={}", homeworkId, studentId);
        HomeworkSubmission submission = homeworkSubmissionService.getByHomeworkIdAndStudentId(homeworkId, studentId);
        return Result.success(submission);
    }
} 